<template>
    <el-dialog
    :title="!dataForm.id? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题" maxlength="100"></el-input>
      </el-form-item>
      <el-form-item label="通知内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="通知内容" type="textarea" maxlength="1000"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orderNum">
        <el-input v-model="dataForm.orderNum" placeholder="排序" type="number"></el-input>
      </el-form-item>
      <el-form-item label="发布状态" prop="status">
        <el-select v-model="dataForm.status" placeholder="请选择">
          <el-option :value="1" label="未发布"></el-option>
          <el-option :value="2" label="已发布"></el-option>
        </el-select>
      </el-form-item> 
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="disabled" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        disabled: false,
        dataForm: {
          id: null,
          title: null,
          content: null,
          orderNum: 1,
          createTime: null,
          status: null
        },
        dataRule: {
          title: [
            {required: true, message: '标题不能为空', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '通知内容不能为空', trigger: 'blur'}
          ],
          orderNum: [
            {required: true, message: '排序不能为空', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请选择发布状态', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || null;
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields();
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/notice/detail/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.item.title;
                this.dataForm.content = data.item.content;
                this.dataForm.orderNum = data.item.orderNum;
                this.dataForm.status = data.item.status;
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.disabled = true;
            this.$http({
                url: this.$http.adornUrl(`/sys/notice/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData({
                  id: this.dataForm.id || undefined,
                  title: this.dataForm.title,
                  content: this.dataForm.content,
                  orderNum: this.dataForm.orderNum,
                  status: this.dataForm.status
                })
            }).then(({data}) => {
              this.disabled = false;
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false;
                    this.$emit('refreshDataList');
                  }
                })
              } else {
                this.$message.error(data.msg);
              }
            })
          }
        })
      }
    }
  }
</script>
